package biolean.logic;

public class Pair<S,T> implements Comparable<Pair<S,T>>
{
    public final S first;
    public final T second;
    final boolean e1Comparable;
    final boolean e2Comparable;

    public Pair(final S e1, final T e2)
    {
        this.first = e1;
        this.second = e2;

        this.e1Comparable = (e1 instanceof Comparable);
        this.e2Comparable = (e2 instanceof Comparable);
    }

    public int compareTo(Pair<S,T> o)
    {
        if (e1Comparable)
        {
            final int k = ((Comparable<S>) first).compareTo(o.first);
            if (k > 0) return 1;
            if (k < 0) return -1;
        }

        if (e2Comparable)
        {
            final int k = ((Comparable<T>) second).compareTo(o.second);
            if (k > 0) return 1;
            if (k < 0) return -1;
        }

        return 0;
    }

    @Override
    public boolean equals(
            Object obj)
    {
        if (obj instanceof Pair)
        {
            final Pair<S,T> o = (Pair<S,T>) obj;
            return (first.equals(o.first) && second.equals(o.second));
        }
        else
        {
            return false;
        }
    }

    @Override
    public int hashCode()
    {
        int hash = 3;
        hash = 19 * hash + (first != null ? first.hashCode() : 0);
        hash = 19 * hash + (second != null ? second.hashCode() : 0);
        return hash;
    }
    
    public String toString()
    {
    	return "[" + first.toString() +"," + second.toString() +"]";
    }
}